

if(!GENTICS.Aloha.Repositories) {
	GENTICS.Aloha.Repositories = {}
}
GENTICS.Aloha.Repositories.internalLink = new GENTICS.Aloha.Repository("com.gentics.aloha.repositories.internalLink","internaLink");

GENTICS.Aloha.Repositories.internalLink.tree = {};
GENTICS.Aloha.Repositories.internalLink.root = null;
GENTICS.Aloha.Repositories.internalLink.init = function() {
	if( GENTICS.Aloha.Repositories.internalLink.settings['treePage'] != null )
		this.tree = GENTICS.Aloha.Repositories.internalLink.settings['treePage'];
		this.root = this.tree["root"]
		delete this.tree["root"]
};

GENTICS.Aloha.Repositories.internalLink.query = function(p, callback) {
	var result = [];
	if( p.inFolderId != null ){
		var folder = p.inFolderId
		if (folder == this.repositoryId )
			folder=this.root;
		folder = this.tree[folder];
		folder = new GENTICS.Aloha.Repository.Document({
		 	id: folder.id,
		 	repositoryId: this.repositoryId,
		 	name: folder.name,
		 	type: 'website',
		 	url: '/'+folder.id+'/'
		 });
		 result.push(folder);
	}else{
		if( p.queryString != null ){
			for( var page in this.tree ){
				item = this.tree[page]
				if( item.name.toLowerCase().indexOf( p.queryString.toLowerCase() ) >= 0){
					console.log("Validaaaaa");
					item = new GENTICS.Aloha.Repository.Document({
					 	id: item.id,
					 	repositoryId: this.repositoryId,
					 	name: item.name,
					 	type: 'website',
					 	url: '/'+item.id+'/'
					 });
					 result.push(item);	
				}
			}
		}
	}
	callback.call(this,result);
};

GENTICS.Aloha.Repositories.internalLink.getChildren = function(p, callback) {
	var folder = p.inFolderId
	if (folder == this.repositoryId )
		folder=this.root;
	
	folder = this.tree[folder];
	var children = []
	for( var c=0; c < folder.children.length; c++){
		child = this.tree[folder.children[c]];
		child = new GENTICS.Aloha.Repository.Folder({
			id : child.id,
			name : child.name,
			parentId : folder.id,
			type : "folder",
			repositoryId : this.repositoryId
		});
		children.push(child);
	}
	callback.call(this,children)
};

